from datetime import datetime

from django.contrib import admin
from django.contrib.admin import SimpleListFilter
from django.http import HttpResponse
from .models import *
import xlwt

# 行
line = 1
# 列
row = 0
# 后台导出为Excel
# @admin.register(Blog)
class WLIndexAdmin(admin.ModelAdmin):
    # 显示列表页字段
    list_display = ['od_number', 'name', 'sex', 'birthday', 'education', 'phone', 'chatNumber', 'address', 'age',
                    'nature_object', 'nature_number', 'sixNatureNumber', 'many_nature', 'nature_time', 'twoMan_nature',
                    'manWoman_nature', 'twoWoman_nature', 'womanWman_nature', 'sexnumber', 'mannumber']
    # 过滤器
    list_filter = ['sex', 'education', 'age', 'nature_object', 'nature_number', 'sixNatureNumber', 'many_nature',
                   'nature_time', 'twoMan_nature', 'manWoman_nature', 'twoWoman_nature', 'womanWman_nature', ]

    # 搜索功能(支持所有字段搜索）
    search_fields = ['od_number', 'name', 'sex', 'birthday', 'education', 'phone', 'chatNumber', 'address', 'age',
                    'nature_object', 'nature_number', 'sixNatureNumber', 'many_nature', 'nature_time', 'twoMan_nature',
                    'manWoman_nature', 'twoWoman_nature', 'womanWman_nature', 'sexnumber', 'mannumber']

    # 导出excel功能
    actions = ['export_excel']

    def export_excel(self, request, queryset):
        """
        后台导出表单数据，导出格式为excel
        :param request:
        :param queryset:
        :return:
        """
        meta = self.model._meta  # 用于定义文件名, 格式为: app名.模型类名
        # 模型所有字段名
        field_names = [field.name for field in meta.fields]
        # 响应内容类型
        response = HttpResponse(content_type='application/vnd.ms-excel')
        # 定义响应数据格式
        response['Content-Disposition'] = 'FormInfo.csv'

        # 开始创建excel
        wb = xlwt.Workbook(encoding='utf-8')
        # 表名
        LST_test = wb.add_sheet('流行病学调查统计表', cell_overwrite_ok=True)
        # excel中的表头
        LST_test.write(0, 0, '序号', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 1, '门诊编号', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 2, '姓名', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 3, '性别', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 4, '出生年月', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 5, '最高学历', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 6, '手机号', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 7, '微信号', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 8, '现居地址', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 9, '初次性行为年龄', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 10, '性行为对象', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 11, '性行为人数', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 12, '近6个月性伴侣人数', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 13, '是否有多人性行为', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 14, '平均性生活时间', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 15, '男-男性方式', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 16, '男-女性方式', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 17, '女-女性方式', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 18, '女-男性方式', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 18, '几人同时', xlwt.easyxf('font: height 240, colour_index red,'))
        LST_test.write(0, 18, '几男几女', xlwt.easyxf('font: height 240, colour_index red,'))

        global line
        global row
        for obj in queryset:
            for field in field_names:
                data = [f'{getattr(obj, field)}']
                print(data)
                LST_test.write(line, row, "".join(data))
                row += 1
            line += 1
            row = 0
        wb.save(response)
        return response

    export_excel.short_description = '导出为Excel'

    # def export_mysql(self, request, queryset):
    #
    #
    # # export_mysql.short

# 注册表单模型
admin.site.register(FormInfo, WLIndexAdmin)
# 注册提交密码模型
admin.site.register(SubmitPassword)